<?php

class Admin_PerformanceDataXtraController extends Zend_Controller_Action
{
    public function locationAction()
    {   
        $id = (int) $this->_request->getParam('id');
        try {
            $form = new Emms_Form_Admin_PerformanceDataXtraLocation($id);
        } catch (Exception $e) {
            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->redirect('/default/auth/deny'); 
        }
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $db = Zend_Db_Table_Abstract::getDefaultAdapter();
                $db->beginTransaction();
                try {
                    $db->delete('performance_data_xtra', array(
                        'LocationId = ?' => $form->getValue('LocationId'),
                        'Date = ?' => date('Y-m-d')));
                    $this->_insert('Sustainability',$form->getValues());
                } catch (Zend_Exception $e) {
                    $db->rollBack();
                    $this->_helper->flashMessenger->addMessage($e->getMessage());
                    $this->redirect('/default/auth/deny'); 
                }
                $db->commit();
                $this->_redirect('/admin/location/view/id/'.$id);
            } else {
                $form->setDescription(
                    $this->view->translate("Operation can't be completed. Please check the form and try again."));
            }  
        } 
        $this->view->form = $form;
        $this->_helper->layout->setLayout('form');
    }
    
    public function officerAction()
    {   
        $id = (int) $this->_request->getParam('id');
        try {
            $form = new Emms_Form_Admin_PerformanceDataXtraOfficer($id);
        } catch (Exception $e) {
            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->redirect('/default/auth/deny'); 
        }
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $db = Zend_Db_Table_Abstract::getDefaultAdapter();
                $db->beginTransaction();
                try {
                    $db->delete('performance_data_xtra', array(
                        'OfficerUserId = ?' => $form->getValue('OfficerUserId'),
                        'Date = ?' => date('Y-m-d')));
                    $this->_insert('Meetings',$form->getValues());
                    $this->_insert('ClientAssistance',$form->getValues());
                    $this->_insert('PersonalGrowth',$form->getValues());
                    $this->_insert('ChurchMinistry',$form->getValues());
                    $this->_insert('Training',$form->getValues());
                } catch (Zend_Exception $e) {
                    $db->rollBack();
                    $this->_helper->flashMessenger->addMessage($e->getMessage());
                    $this->redirect('/default/auth/deny'); 
                }
                $db->commit();
                $this->_redirect('/admin/officer/view/id/'.$id);
            } else {
                $form->setDescription(
                    $this->view->translate("Operation can't be completed. Please check the form and try again."));
            }  
        } 
        $this->view->form = $form;
        $this->_helper->layout->setLayout('form');
    }
    
    private function _insert($metric,$data)
    {
        $dataXtra = new Zend_Db_Table(array(
                    'name' => 'performance_data_xtra', 
                    'primary' => 'PerformanceDataXtraId'));
        $dataXtra->insert(array(
            'OfficerUserId' => isset($data['OfficerUserId']) ? $data['OfficerUserId'] : null,
            'LocationId' => isset($data['LocationId']) ? $data['LocationId'] : null,
            'Metric' => $metric,
            'Value' => $data[$metric],
            'Date' => date('Y-m-d'),
            'LastEditDate' => $data['LastEditDate'],
            'LastEditUserId' => $data['LastEditUserId']
        ));
    }
}
